// pages/noticeMore/noticeMore.js
const db = wx.cloud.database();
const app = getApp()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    notice: {},
    fontSize: "19px",
    font: "改大号",
    //收藏数据
    collectionUrl: '',
    isClick: false,
    openid: '',
    flag: false,
    allcollectionList: [],
    currentUrl: '',
    collectionTitle: ''
  },

  fontChange() {
    this.setData({
      fontSize: this.data.fontSize === "19px" ? "25px" : "19px",
      font: this.data.font === "改大号" ? "改默认" : "改大号"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.comment) {
      setTimeout(() => {
        this.setData({
          scrollTop: "comment",
        })
      }, 1200)
    }
    this.setData({
      title: options.title === "index" ? "通知公告" : "技能培训",
      id:options._id,
      flag: options.title === "index" ? false : true,
      openid: app.globalData.openid,
      currentUrl: options._id
    })
    let url
    if (options.title === "index") {
      url = "index_noticeList"
    } else {
      url = "training_moreList"
    }
    db.collection(url).doc(options._id).get({
      success: (res) => {
        console.log(res.data);
        this.setData({
          notice: res.data,
          // currentUrl:`${res.data._id}`
        })
      }
    })
    this.checkCollection();
  },
  checkCollection() {
    if (this.data.flag) {
      let that = this;
      db.collection('collectionurlList').where({
        _openid: this.data.openid
      }).get({
        success(res) {
          that.setData({
            allcollectionList: res.data
          })
        }
      })
      setTimeout(() => {
        for (var i = 0; i < that.data.allcollectionList.length; i++) {
          if (this.data.allcollectionList[i].collectionUrl == this.data.currentUrl) {
            console.log(this.data.allcollectionList[i].collectionUrl);
            console.log(this.data.currentUrl);
            console.log("就是他");
            this.setData({
              isClick: true
            })
          }
        }
      }, 1000)
    }
  },
  //到底时让评论拉取更多数据
  onReachBottom() {
    this.selectComponent('.comment').onReachBottom()
  },
  //收藏
  haveSave(e) {
    //点击收藏
    if (!this.data.isClick == true) {
      this.setData({
        collectionUrl: `${this.data.notice._id}`,
        collectionTitle: this.data.notice.title,
        isClick: true
      })
      let that = this;
      db.collection('collectionurlList').where({
        _openid: app.globalData.openid,
        collectionUrl: that.data.collectionUrl
      }).get({
        success(res) {
          if (res.data.length == 0) {
            db.collection('collectionurlList').add({
              data: {
                type: that.data.title,
                collectionUrl: that.data.collectionUrl,
                collectionTitle: that.data.collectionTitle
              }
            })
          } else {}
        }
      })
      wx.showToast({
        title: '已收藏',
      });
    } else {
      this.setData({
        collectionUrl: '',
        isClick: false
      })
      db.collection('collectionurlList').where({
        _openid: this.data.openid,
        collectionUrl: this.data.currentUrl,
      }).remove({
        success(res) {
          console.log(res);
        }
      })
      wx.showToast({
        title: '已取消收藏',
      })
    }
    console.log(this.data.collectionUrl);
  },
  //页面销毁时根据收藏状态，向数据库添加页面路径
  // onUnload(){
  // let that=this;
  // if(that.data.isClick == true &&that.data.collectionUrl!=''){
  // db.collection('collectionurlList').add({
  //             data:{
  //           collectionUrl:that.data.collectionUrl,
  //           collectionTitle:that.data.collectionTitle
  //         }
  // })
  //   db.collection('collectionurlList').where({
  // _openid:that.data.openid
  //   }).get({
  //         success(res){
  //           if(res.data.length!=0){
  //             console.log(res.data);
  //             db.collection('collectionurlList').doc(res.data[0]._id).update({
  //               data:{
  //                 collectionUrl:_.push(that.data.collectionUrl),
  //                 title:_.push(that.data.collectionTitle)
  //               },
  //             })
  //           }else{
  //         db.collection('collectionurlList').add({
  //           data:{
  //             collectionUrl:that.data.collectionUrl,
  //             title:that.data.collectionTitle
  //           }

  //         })
  //           }
  //         }
  //   })
  //确定取消收藏
  // }else if(that.data.isClick == false){
  // db.collection('collectionurlList').where({
  //   _openid:this.data.openid,
  //   collectionUrl:that.data.currentUrl,
  // }).remove({
  //   success(res){
  //     console.log(res);
  //   }
  // })
  // }
  // }
})